package com.hub.nio.api;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.concurrent.TimeUnit;

/**
 * @ author 令狐冲
 * @ date & time 2025/2/8 14:40
 */
@RestController
public class HelloApi {

    @GetMapping("/echo")
    public Flux<Object> sayHelloWorld() {

        Flux<Object> objectFlux = Flux.create(sink -> {
            for (int i = 1; i <= 10; i++) {
                sink.next(i);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (i == 5) {
                    sink.complete();
                }
            }
        });
        return objectFlux;
    }
}
